/**
 * 
 */
package com.calf.module.jpush;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.audience.AudienceTarget;
import cn.jpush.api.push.model.notification.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * @ClassName JPushAllUtil
 * @Description 极光推送工具类
 * @author cx
 * @Date 2017年3月13日 下午8:42:17
 * @version 1.0.0
 */
public class JPushUtilxml {
    private final static String appKey = "83be72283d7650ef2b76945b";
    private final static String masterSecret = "6833eb55f0486f847edaead8";
//    private final static String ALERT = "您有一条新的消息!小牛速贷";
//    private final static String SENDNO = "";
    /** 
     * 测试方法 
     */  
    public static void main(String[] args) {  
        //发送通知   1 产品,产品productid  2 文章airtldid  
    	
    /*	170525170848246 
    	1678f144b4dd418e8195335f064ed2d8-星星钱袋-1
    	
    	(String alias, String alert, String title, String url,String target,String productId,String hitsId,String linkUrl,String productName,String isRecommend
    	*///2c4ac4baa53d4bd8a6b0f77c7050d94e 1_860046036922831
    	//https://testwww.xnsudai.com/html/page/article_detail.html?articleId=a54179a6716d47f1bf0717a813768df0
    	  jSend_notification("f7b73af6b0424925a986fd06bde266f0,1_A00000598B1C9A,0_091c9b69e945479a83a7a71bf154681e","【小牛速贷】您有一条消息！","测试","http://www.baidu.com","3","","170525170848246","xulugj://www.xulu.com/jump?need_login=0&url=https://testwww.xnsudai.com/html/page/guanjia_invite.html&activityId=1","","1","小额贷款公司有哪些优势？这五点您都知道么！");
    	for(;;){
   		 try {
				Thread.sleep(50000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
   		 jSend_notification("f7b73af6b0424925a986fd06bde266f0,1_A00000598B1C9A,0_091c9b69e945479a83a7a71bf154681e","【小牛速贷】您有一条消息！","测试","http://www.baidu.com","3","","170525170848246","xulugj://www.xulu.com/jump?need_login=0&url=https://testwww.xnsudai.com/html/page/guanjia_invite.html&activityId=1","","1","小额贷款公司有哪些优势？这五点您都知道么！");
    	}
    
    	
    }  
    /** 
     * 发送通知 
     * @param alias 设备标识 
     * @param alert 推送内容 
     */  
    public static void jSend_notification(String alias, String alert, String title, String url,String target,String productId,String hitsId,String linkUrl,
    		String productName,String isRecommend,String articleTitle
    		){  
        JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
//        PushPayload payload = send_N(registrationId, alert);  
        PushPayload payload = buildPushObject_all_alias_alert_(alias, alert, title, url,target,productId,hitsId,linkUrl,productName,isRecommend,articleTitle);
        try {  
            PushResult result = jpushClient.sendPush(payload);
            System.out.println(result);
              
        } catch (APIConnectionException e) {
            System.out.println(e);
        } catch (APIRequestException e) {
            System.out.println(e);  
            System.out.println("Error response from JPush server. Should review and fix it. "+ e);  
            System.out.println("HTTP Status: " + e.getStatus());  
            System.out.println("Error Code: " + e.getErrorCode());  
            System.out.println("Error Message: " + e.getErrorMessage());  
            System.out.println("Msg ID: " + e.getMsgId());  
        }  
    }  
    
    /**
     * 
     * @Description 给所有平台所用设备推送
     * @param alert 推送内容
     * @return
     */
    public static PushPayload buildPushObject_all_all_alert(String alert) {
        return PushPayload.alertAll(alert);
    }
    
    /**
     * 
     * @Description 针对所有用户发送根据别名单个发送
     * @param alias 用户别名
     * @param alert 推送内容
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert(String alias, String alert) {
    	String [] strs = alias.split(",");
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.alias(strs))
                .setNotification(Notification.alert(alert))
                .build();
    }
    
    /**
     * 
     * @Description 针对所有用户发送根据不同情况发送通知
     * @param alias 用户别名
     * @param alert 推送内容
     * @return
     */
    public static PushPayload buildPushObject_all_alias_alert_(String alias, String alert, String title,
                                                               String url, String target, String productId, String hitsId, String linkUrl, String productName, String isRecommend, String articleTitle) {
    	String [] strs = alias.split(",");
    	 return PushPayload.newBuilder()
                 .setPlatform(Platform.android_ios())
                 .setAudience(Audience.alias(strs))
                 .setNotification(Notification.newBuilder()
                         .addPlatformNotification(AndroidNotification.newBuilder()
                                 .setAlert(alert)
                                 .addExtra("from", "JPush")
                                 .addExtra("url", url)
                                 .addExtra("title", title)
                                 .addExtra("target", target) //1 产品详情页 2文章详情页
                                 .addExtra("id", productId)   //target=1-id就是产品详情 ,target=2-就是文章详情
                                 .addExtra("hitsId", hitsId)   //push通知id
                                 .addExtra("linkUrl", linkUrl) //文章拼接url
                                 .addExtra("productName",productName)   //产品name
                                 .addExtra("isRecommend",isRecommend)  //是否推荐
                                 .addExtra("articleTitle", articleTitle)
                         .build())
                         .addPlatformNotification(IosNotification.newBuilder()
                                 .setAlert(alert)
                                 .setBadge(5)
                                 .setSound("happy")
                                 .addExtra("from", "JPush")
                                 .addExtra("url", url)
                                 .addExtra("title", title)
                                 .addExtra("target", target)
                                 .addExtra("id", productId)
                                 .addExtra("hitsId", hitsId)   //push通知id
                                 .addExtra("linkUrl", linkUrl) //文章拼接url
                                 .addExtra("productName",productName)   //产品name
                                 .addExtra("isRecommend",isRecommend)  //是否推荐
                                 .addExtra("articleTitle", articleTitle)
                                 .build())
                         .build())
//                  .setMessage(Message.content(alert))
//                  .setOptions(Options.newBuilder()
//                          .setApnsProduction(true)
//                          .build())
                  .build();
    }
    
    /**
     * 
     * @Description 针对所有用户发送给多个用户推送
     * @param aliass 用户别名集合
     * @param alert 推送内容
     * @return
     */
    public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras(String aliass, String alert) {
    	String [] strs = aliass.split(",");
    	return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.newBuilder()
                        .addAudienceTarget(AudienceTarget.alias(strs))
                        .build())
                        .setMessage(Message.newBuilder()
                                .setMsgContent(alert)
                                .addExtra("from", "JPush")
                                .addExtra("url", "http://www.baidu.com")
                                .build())
                        .build();
    }
    
    
}
